import csv
import datetime
import time
import traceback

import jsonpath
import requests


class GetInfo():
    def __init__(self, ip=None, port=None):
        self.ip = ip
        self.port = port

        # 是否是第一次写入
        self.is_one_w = True

        self.headers_s = ['region_name', 'block_name', 'rhval', 'area', 'price', 'long_title', 'comm_name',
                          'house_orient_name', 'floor_tag']

    def start(self, n):
        '''运行程序'''
        for i in range(1, n + 1):
            # 生成url
            url = self.url(i)
            # 生成 headers
            headers = self.headers()
            # 生成proxies
            proxies = self.proxies()

            # 发送请求
            response = self.requests(url, headers, proxies)

            # 清洗数据
            data = self.result(response)
            # 数据保存
            ret = self.save(data)
            if ret:
                print("%s页请求完成" % i)

    def url(self, p):
        url = "https://shuyang.anjuke.com/v3/ajax/map/sale/857/prop_list/?p=%s" % str(p)

        return url

    def proxies(self):
        '''获取proxies'''
        if self.ip and self.port:
            ipret = isinstance(self.ip, str)
            if not ipret:
                raise Exception("ip类型应该是str")
            portret = isinstance(self.port, int)
            if not portret:
                raise Exception("port类型应该是int")
        else:
            return False

    def headers(self):
        headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
        }
        return headers

    def cookies(self):
        '''获取cookie'''
        return

    def result(self, response):
        '''清洗数据'''
        try:
            # 获取json数据
            data_info = response.json()

        except:
            raise Exception("请求数据格式有误")

        dict_list = jsonpath.jsonpath(data_info, '$.val.props')

        if not dict_list:
            raise Exception("未获取到数据")
        # 定义数据总的列表
        rows = []

        for j in dict_list[0]:  # 每一条数据的字典
            # 每一行数据
            row = []

            for x in self.headers_s:
                row.append(j.get(x))

            rows.append(row)

        return rows

    def save(self, data):

        '''保存数据'''

        # 生成当前时间
        time_ = datetime.datetime.now().strftime('%Y-%m-%d').replace("-", "")

        try:
            with open('沭阳房产_%s' % time_ + ".csv", 'a+', encoding='utf-8') as f:

                f_csv = csv.writer(f)

                if self.is_one_w:
                    # 写入表头
                    f_csv.writerow(self.headers_s)
                    self.is_one_w = False
                # 数据
                f_csv.writerows(data)

        except Exception:
            traceback.print_exc()
            raise Exception("写入文件产生错误")

        return True

    def requests(self, url, headers, proxies):

        time.sleep(2)
        '''发送请求'''
        try:
            response = requests.get(url=url, headers=headers, proxies=proxies)
        except Exception:
            raise Exception("请求失败")

        if response.status_code != 200:
            print("状态码:", response.status_code)

        return response


if __name__ == '__main__':
    g = GetInfo()

    g.start(5)
